//
//  AuthServer.m
//  HGMMRAC
//
//  Created by eport on 2021/1/19.
//

#import "AuthServer.h"
#import <AFNetworking/AFNetworking.h>

@implementation AuthServer


+(void)loginUserName:(NSString *)userName
            passWord:(NSString *)password
             Success:(void (^)( NSDictionary *resultDic))success
             failure:(void (^)( NSError* error))failure
               state:(void (^)( NSString *reqestState))state{
    userName =   [userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString  *device = @"iPhone 6s";
    NSString *imei = @"5864142545151";
    NSString*strURL=[NSString stringWithFormat:@"%@api/auth/login",@"http://auth.Mobile.customs.gov.cn:7001/HGAuth/"];
    NSDictionary*parma=@{@"action":@"DO",@"hgflag":userName,@"password":@"731982A033A5CC815AC03C8504ABB748",@"imei":imei,@"device":device};
    
    
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    
    sessionManager.requestSerializer  = [AFHTTPRequestSerializer serializer];
       
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
       sessionManager.requestSerializer.timeoutInterval = 20;
    [sessionManager POST:strURL parameters:parma headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSError *error = nil;
            NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:(NSData*)responseObject options:NSJSONReadingAllowFragments error:&error];
        
        if (![resultDictionary objectForKey:@"status"]) {
            
            failure([NSError errorWithDomain:resultDictionary[@"error"] code:[resultDictionary[@"code"] intValue] userInfo:nil]);
            
        }else{
            success(resultDictionary);
            [[NSUserDefaults standardUserDefaults]setObject:resultDictionary[@"token"] forKey:@"token"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
        }
        
            
            
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
            
    }];
       
    

}



@end
